package com.it.blog.controller;

import com.github.pagehelper.PageInfo;
import com.it.base.utils.R;
import com.it.blog.domain.ArticleType;
import com.it.blog.query.ArticleTypeQuery;
import com.it.blog.service.ArticleTypeService;
import com.it.blog.vo.ArticleTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {
    @Autowired
    private ArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation("查询所有文章类型")
    public R<List<ArticleType>> findAll() {
        List<ArticleType> articleTypeList = articleTypeService.findAll();
        return R.ok(articleTypeList);
    }

    @PostMapping
    @ApiOperation("分页条件查询文章类型")
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery) {
        PageInfo<ArticleType> pageInfo=articleTypeService.queryPage(articleTypeQuery);
        return pageInfo;
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询文章类型")
    public R<ArticleType> findOne(@PathVariable Long id) {
        ArticleType articleType=articleTypeService.findOne(id);
        return R.ok(articleType);
    }

    @PutMapping
    @ApiOperation("增加OR修改文章类型")
    public R<Void> addOrUpdateArticleType(@RequestBody ArticleType articleType) {
        try {
            Long id = articleType.getId();
            if (id == null) {
                articleTypeService.addArticleType(articleType);
            } else {
                articleTypeService.updateArticleType(articleType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除文章类型")
    public R<Void> deleteArticleType(@PathVariable Long id) {
        try {
            articleTypeService.deleteArticleType(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("根据id批量删除文章类型")
    public R<Void> batchArticleTypes(@RequestBody Long[] ids) {
        try {
            articleTypeService.batchArticleTypes(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/getTypeTree/{id}")
    @ApiOperation("查询文章类型树")
    public R<List<ArticleType>> getTypeTree(@PathVariable Long id) {
        List<ArticleType> articleTypeList=articleTypeService.getTypeTree(id);
        return R.ok(articleTypeList);
    }

    @GetMapping("/articleTypeStatistics")
    @ApiOperation("文章类型统计")
    public R<List<ArticleTypeVo>> getArticleTypeStatistics(){
        List<ArticleTypeVo> articleTypeList=articleTypeService.getArticleTypeStatistics();
        return R.ok(articleTypeList);
    }
}
